package com.cqemme.comm.utils;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;


/**
 * @author 86156
 */
public class Rc4 {
	
	public static String SerKey="931e777a-476e-4339-8ead-b8b5bab61d75";
	
	
	/**
	 * RC4加密
	 * @param context
	 * @param key
	 * @return
	 */
	@SuppressWarnings("finally")
	public static String encodeByKey(String context,String key){
		String ciphertext = null;
		 try {
			Cipher cipher=Cipher.getInstance("RC4");
			 SecretKeySpec rkey=new SecretKeySpec(key.getBytes("UTF-8"), "RC4");
			 cipher.init(Cipher.ENCRYPT_MODE, rkey);
			 byte[] cdata =cipher.update(context.getBytes("UTF-8"));
			 ciphertext = parseByte2HexStr(cdata);
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}finally{
			return ciphertext;
		}
	}
	
	/**
	 * RC4解密
	 * @param context
	 * @param
	 * @return
	 */
	public static String decryptByKey(String context,String keyPwd){
		
			try {
				Cipher cipher= Cipher.getInstance("RC4");
				SecretKeySpec key=new SecretKeySpec(keyPwd.getBytes("UTF-8"), "RC4");
				cipher.init(Cipher.DECRYPT_MODE, key);
				byte[] cdata = parseHexStr2Byte(context);
				byte[] ddata =cipher.update(cdata);      
				String str = new String(ddata,"UTF-8");
				return str;
			} catch (InvalidKeyException e) {
				e.printStackTrace();
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchPaddingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		
		
		return null;
	}
	
	
    /**  
    * 将二进制转换成16进制  
    *   
    * @param buf  
	* @return  
	*/  
	 public static String parseByte2HexStr(byte buf[]) {  
	     StringBuffer sb = new StringBuffer();  
	     for (int i = 0; i < buf.length; i++) {  
	         String hex = Integer.toHexString(buf[i] & 0xFF);  
	         if (hex.length() == 1) {  
	             hex = '0' + hex;  
	         }  
	         sb.append(hex.toUpperCase());  
	     }  
	     return sb.toString();  
	 }  
 
	 /**  
	  * 将16进制转换为二进制  
	  *   
	  * @param hexStr  
	  * @return  
	  */  
	 public static byte[] parseHexStr2Byte(String hexStr) {  
	     if (hexStr.length() < 1)  {
			 return null;
		 }

	     byte[] result = new byte[hexStr.length() / 2];  
	     for (int i = 0; i < hexStr.length() / 2; i++) {  
	         int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);  
	         int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),  
	                 16);  
	         result[i] = (byte) (high * 16 + low);  
	     }  
	     return result;  
	 }  
//	public static void main(String[] args) {
//		try {
//			String uuid = "931e777a-476e-4339-8ead-b8b5bab61d75";
//			Cipher cipher= Cipher.getInstance("RC4");
//			String pwd=uuid.substring(0, 16);
//			System.out.println(pwd.length());
//			String ptext="ac=gufs&uname=sss&upass=123123";
//
//			SecretKeySpec key=new SecretKeySpec(pwd.getBytes("UTF-8"), "RC4");
//			cipher.init(Cipher.ENCRYPT_MODE, key);
//			byte[] cdata =cipher.update(ptext.getBytes("UTF-8"));
//			//解密
//			cipher.init(Cipher.DECRYPT_MODE, key);
//			byte[] ddata =cipher.update(cdata);
//			System.out.println("密码: "+pwd);
//			System.out.println("明文: "+ptext);
//			System.out.println("密文: "+DatatypeConverter.printHexBinary(cdata));
//			System.out.println("解密文: "+new String(ddata,"UTF-8"));
//
//			System.out.println("===============================");
//			String s1 = RC4.encodeByKey(ptext, pwd);
//			System.out.println("密文: "+s1);
//			System.out.println("明文: "+RC4.decryptByKey(s1, pwd));
//		} catch (InvalidKeyException e) {
//			e.printStackTrace();
//		} catch (NoSuchAlgorithmException e) {
//			e.printStackTrace();
//		} catch (NoSuchPaddingException e) {
//			e.printStackTrace();
//		} catch (UnsupportedEncodingException e) {
//			e.printStackTrace();
//		}
//
//	}
}
